import requests

from lxml import etree

# 请求头
head = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36"
}

# 请求页面获取网页数据

url = "https://movie.douban.com/top250?start=%7Bpage%7D&filter="
response = requests.request(url=url, method="get", headers=head)

# 获取响应的内容
text = response.text


# 解析html
etree_html = etree.HTML(text)

# 获取所有影片
items = etree_html.xpath("//ol[@class='grid_view']/li")

# 遍历解析影片
for item in items:
    name = item.xpath("./div/div/div/a/span/text()")[0]
    com = item.xpath("./div/div/div/div/span/text()")
    score = com[0]
    com_num = com[1]
    dy = item.xpath("./div/div/div/p/text()")
    dy1 = dy[0].strip()
    year = dy[1].strip()
    print(name, score, com_num, dy1, year)
